package http

import (
	"gin-web/app"
	"gin-web/app/http/middleware/logger"
	"gin-web/app/http/routes"
	"gin-web/app/utils/customValidate"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

// NewService 启动服务入口
func NewService() error {
	r := gin.New()

	//使用自定义请求日志
	r.Use(logger.New(app.Config.ServerLog, app.Config.PrjHome))
	//使用panic捕获中间件
	r.Use(gin.Recovery())

	//注册自定义验证器
	customValidate.RegisterCustomValidate()

	//设置允许跨域
	r.Use(cors.New(cors.Config{
		AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"},
		AllowHeaders: []string{"Origin", "Content-Length", "Content-Type", "Authorization"},
		//AllowOrigins: []string{"http://localhost:3000", "https://localhost:3000"},
		AllowAllOrigins: true,
	}))
	//注册路由
	routes.Register(r)

	return r.Run(app.Config.Http.Listen)
}
